import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import jwt from 'jsonwebtoken';

Vue.use(VueRouter)

// export let mainRoute = [
//   {
//     path: '/Home/User',
//     name: 'User',
//     component: () => import('../views/Home/User.vue'),
//     txt:'用户管理',
//     icon:'manager',
//   },
//   {
//     path: '/Home/Menu',
//     name: 'Menu',
//     component: () => import('../views/Home/Menu.vue'),
//     txt:'菜单管理',
//     icon:'bars',
//   },
//   {
//     path: '/Home/Role',
//     name: 'Role',
//     component: () => import('../views/Home/Role.vue'),
//     txt:'角色管理',
//     icon:'friends',
//   },
//   {
//     path: '/Home/Type',
//     name: 'Type',
//     component: () => import('../views/Home/Type.vue'),
//     txt:'角色类型管理',
//     icon:'gem',
//   },
//   {
//     path: '/Home/Operation',
//     name: 'Operation',
//     component: () => import('../views/Home/Operation.vue'),
//     txt:'操作日志',
//     icon:'setting',
//   }
// ]
const routes = [
  {
    path: '/',
    redirect: '/Home/User',  // 重定向:重新指向其它path,会改变网址
  },
  {
    path: '/Home',
    name: 'Home',
    component: Home,
    children:[
      {
        path: '/Home/User',
        name: 'User',
        component: () => import('../views/Home/User.vue'),
        
      },
      {
        path: '/Home/Menu',
        name: 'Menu',
        component: () => import('../views/Home/Menu.vue'),
        
      },
      {
        path: '/Home/Role',
        name: 'Role',
        component: () => import('../views/Home/Role.vue'),
        
      },
      {
        path: '/Home/Type',
        name: 'Type',
        component: () => import('../views/Home/Type.vue'),
      
      },
      {
        path: '/Home/Operation',
        name: 'Operation',
        component: () => import('../views/Home/Operation.vue'),
        
      }
    ]
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


// 动态路由
const NewRoute = [
  {
    path: '/Login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/Add',
    name: 'Add',
    component: () => import('../views/Add.vue')
  },
  {
    path: '/Edit/:id',
    name: 'Edit',
    component: () => import('../views/Edit.vue')
  }
]
NewRoute.forEach((item,index)=>{
  router.addRoute(item)
})
// 白条
const widthPath = ['/Login','/Add','/Edit/:id']
// 导航守卫
router.beforeEach((to, from, next) => {
  const token = window.localStorage.getItem('token')
  if(token || widthPath.includes(to.path)){
    next()
  }else{
    next('/Login')
  }
})

export default router
